package com.code2roc.jarmanage.util;

import com.code2roc.jarmanage.depoly.DepolyPlatform;
import com.code2roc.jarmanage.exception.LostJavaHomeConfigException;
import com.code2roc.jarmanage.exception.UnSupportSystemException;

public class EnviromentUtil {
    private static String platformName = "";

    public static void checkEnvInstall() {
        String platform = getDepolyPlaform();
        if (platform.equals(DepolyPlatform.UnDefined)) {
            throw new UnSupportSystemException();
        }
        LogUtil.info("【DepolyPlatform】：" + platform);
        String javaHomePath = getJavaHome();
        if (StringUtil.isEmpty(javaHomePath)) {
            throw new LostJavaHomeConfigException();
        }
        LogUtil.info("【JavaHome】：" + javaHomePath);
    }

    public static String getDepolyPlaform() {
        if(StringUtil.isEmpty(platformName)){
            String platform = DepolyPlatform.UnDefined;
            String os = System.getProperty("os.name").toLowerCase();
            if (os.startsWith("win")) {
                platform = DepolyPlatform.Windows;
            } else if (os.startsWith("linux")) {
                String command = "cat /etc/system-release";
                String result = CMDUtil.executeLinuxCommand(command);
                if (result.startsWith("Red Hat")) {
                    platform = DepolyPlatform.LinuxRedHat;
                } else if (result.startsWith("CentOS")) {
                    platform = DepolyPlatform.LinuxCentOS;
                } else if (result.startsWith("openEuler")) {
                    platform = DepolyPlatform.LinuxOpenEuler;
                } else if(result.startsWith("Fedora")){
                    platform = DepolyPlatform.LinuxFedora;
                }else {
                    command = "cat /etc/issue";
                    result = CMDUtil.executeLinuxCommand(command);
                    if (!StringUtil.isEmpty(result)) {
                        if (result.startsWith("Ubuntu")) {
                            platform = DepolyPlatform.LinuxUbuntu;
                        } else if (result.startsWith("Debian")) {
                            platform = DepolyPlatform.LinuxDebian;
                        }
                    }
                }
            }
            platformName = platform;
        }
        return platformName;
    }

    public static String getJavaHome() {
        String os = System.getProperty("os.name").toLowerCase();
        String javaHomePath = "";
        if (os.startsWith("win")) {
            String command = "echo %JAVA_HOME%";
            String result = CMDUtil.executeWindowCommand(command);
            if (result.equals("%JAVA_HOME%")) {
                javaHomePath = "";
            } else {
                javaHomePath = result;
            }

        } else {
            String command = "echo $JAVA_HOME";
            javaHomePath = CMDUtil.executeLinuxCommand(command);
        }
        return javaHomePath;
    }

    public static boolean checkPHPEnvINstall(){
        String command = "php -v";
        String result = "";
        String os = System.getProperty("os.name").toLowerCase();
        if (os.startsWith("win")) {
            result = CMDUtil.executeWindowCommand(command);
        }else{
            result = CMDUtil.executeLinuxCommand(command);
        }
        return result.toLowerCase().contains("php group");
    }
}
